import { Component, OnInit } from '@angular/core';
import { Router, NavigationExtras, ActivatedRoute } from '@angular/router';
import { AlertController } from '@ionic/angular';
import {HttpClient,HttpHeaders} from "@angular/common/http";
import {TranslateService} from '@ngx-translate/core';
import { DefaultLanguageService } from '@services/default-language.service';
import { CommonResponse,VerificationType,ToastColor,CommonService,AppParam } from '@services/common-service.service';
@Component({
  selector: 'app-message-notification',
  templateUrl: './message-notification.page.html',
  styleUrls: ['./message-notification.page.scss'],
})
export class MessageNotificationPage implements OnInit {
  notification:any;
  i18n:any;

  constructor(
    private router:Router,
    private alertController:AlertController,
    private activatedRoute:ActivatedRoute,
    private http:HttpClient,
    private translate: TranslateService,
    private defaultLanguageService:DefaultLanguageService,
    private commonService:CommonService
  ){
    let defultLanguage = this.defaultLanguageService.getDefaultLanguage();
    this.translate.setDefaultLang(defultLanguage);
    this.translate.use(defultLanguage);
    this.translate.get('myAccount.setting.message_notification_setting').subscribe((value)=>{
      this.i18n = value;
      this.initPages();
    });

    // this.i18n = this.commonService.initTranslate(['myAccount.setting.message_notification_setting']);
  

    this.activatedRoute.data.subscribe((obj)=>{
      let body = obj.data.responseBody;
      this.notification = body;
      this.options.forEach((item,index)=>{
        if(index==0){
          item.optionEnable = body.sysNotification;
        }else if(index==2){
          item.optionEnable = body.txnNotification;
        }

      });
    });
  }

  ngOnInit(){

  }
  options = [];
  initPages(){
    this.options = [
      { 
        id:'1',
        title:this.i18n.system_notification,
        icon:'',
        name:'sysNotification',
        path:'',
        toggle:true,
        changeEnable:(evt,option)=>{
          this.changeOptionToggle(evt,option);
        }
      },
      {
        id:'2',
        title:this.i18n.duedate_reminder,
        name:'dateReminder',
        icon:'',
        path:'/tabs/myAccount/setting/message-notification/duedate-reminder',
        toggle:false,
        optionEnable:false
      },
      {
        id:'3',
        name:'txnNotification',
        title:this.i18n.transaction_notifications,
        icon:'',
        path:'',
        toggle:true,
        optionEnable:false,
        changeEnable:(evt,option)=>{
          this.changeOptionToggle(evt,option);
        }
      }
    ];
  }

  changeOptionToggle(evt,option){
    let api = '/scm/app/updateMessageNotification.do';
    let requestData:any = {
      appUserId:'123'
    };
    switch (option.name) {
      case 'txnNotification':
        requestData.txnNotification = option.optionEnable ? 1 : 0;
        break;
    
        case 'sysNotification':
        requestData.sysNotification = option.optionEnable ? 1 : 0;
        break;
    }

    // console.info(evt);
    // console.info(option);
    this.http.post(api,requestData).subscribe((response) => {});
    if (evt.detail.checked) {
      option.optionEnable = true;
    }else{
      option.optionEnable = false;
    }
  }

  showDetail(id){
    const option = this.options.find(tech => tech.id === id);
    if(option.path){
      let navigationExtras: NavigationExtras = {
        queryParams: this.notification
      };
      this.router.navigate([option.path]);
    }
  }
}
